<template>
  <el-form>
    <el-row  :gutter="20">
    <div >
      <el-col :span="4" :offset="8">
      <el-avatar class="myavatar" :src="user.avatar"></el-avatar>
      </el-col>
      <el-col :span="4">
       <el-upload
        style="padding-top:20px;"
          class="avatar-uploader"
          :action="actionUrl" 
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload">
          
          <i  class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
        </el-col>
    </div>
    </el-row>
    <el-form-item label="姓名">
      <el-input v-model.trim="user.userName" />
    </el-form-item>
    <el-form-item label="邮箱">
      <el-input v-model.trim="user.userEmail" />
    </el-form-item>
    <el-form-item label="个性签名">
      <el-input v-model.trim="user.userSign" />
    </el-form-item>
    <el-form-item label="手机号码">
      <el-input v-model.trim="user.userPhone" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="updateUser">修改信息</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { updateUser,getInfo } from '@/api/user'
export default {
  data() {
    return {
      user:{
        avatar:'',
        userName:'',
        userEmail:'',
        userSign:'',
        userPhone:'',
        userAccount:''
      },
      actionUrl:process.env.VUE_APP_BASE_UPLOAD
    }
  },
  //页面打开时执行的方法
  created() {
    this.user.avatar =this.$store.state.user.avatar
   this.user.userName =this.$store.state.user.userName
   this.user.userEmail =this.$store.state.user.userEmail
   this.user.userPhone =this.$store.state.user.userPhone
   this.user.userAccount =this.$store.state.user.userAccount
   this.user.userSign = this.$store.state.user.userSign
  },
  methods: {
    async updateUser() {
      await updateUser(this.user).then(()=>{
            this.$message({
              type: 'success',
              message: '修改个人信息成功!'
            })
            this.$router.go(0)
            }
          ).catch(err=>{
            this.$message({
              type: 'error',
              message: '修改个人信息失败!'
            })
            })
    },
     handleAvatarSuccess(res, file) {
        this.user.avatar = res.bean.url
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      }
  }
}
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 40px;
    height: 40px;
    line-height: 40px;
    text-align: center;
    
  }
  .myavatar{
    width: 80px;
    height: 80px;
  }
</style>